import java.io.*;
import java.util.ArrayList;
import java.util.StringTokenizer;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Lily
 * Date: 2024-11-08
 * Time: 12:03
 */
public class test_3 {
    //PrintWriter里面的方法和System.out的使用方法一样
    public static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    public static Read in = new Read();
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     * @param m int整型ArrayList<ArrayList<>>
     * @return int整型
     */
    static int m, n;
    static int[] dx = {1, -1, 0, 0};
    static int[] dy = {0, 0, 1, -1};
    static boolean[][] view;
    int ret = 0;

    public int citys(ArrayList<ArrayList<Integer>> city) {
        m = city.size();
        n = city.get(0).size();
        view = new boolean[m][n];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (!view[i][j] && city.get(i).get(j) != 0) {
                    dfs(city, i, j);
                    ret++;
                }
            }
        }
        return ret;
    }

    private void dfs(ArrayList<ArrayList<Integer>> city, int i, int j) {
        if (i < 0 || i >= m || j < 0 || j >= n && city.get(i).get(j) != 0) {
            return;
        }
        view[i][j] = true;
        for (int k = 0; k < 4; k++) {
            int nx = i + dx[k];
            int ny = j + dy[k];
            if (nx >= 0 && nx < m && ny >= 0 && ny < n && !view[nx][ny] && city.get(nx).get(ny) != 0) {
                dfs(city, nx, ny);
            }
        }
    }

}

//快读模板
class Read {//自定义快读读入
    //字符串裁剪
    StringTokenizer st = new StringTokenizer("");

    //1.字节流System.in->字符流InputStreamReader
    //2.带内存缓冲区的字符流BufferedReader
    BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));


    String next() throws IOException {
        //为什么要while循环，因为可能读取的数据不止一行，有多行就需要多次取
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(bf.readLine());//先拿一行的数据
        }
        return st.nextToken();//对这一行的数据进行裁剪，拿到第一个数据
    }

    String nextLine() throws IOException {
        return bf.readLine();
    }

    int nextInt() throws IOException {
        return Integer.parseInt(next());
    }

    long nextLong() throws IOException {
        return Long.parseLong(next());
    }

    double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }
}